package com.htandroid.htandroid.views;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.htandroid.htandroid.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

/**
 * Created by caih on 2018/7/5.
 */
public class MySelectedDialogFragmentTime extends DialogFragment {

    private TextView tv_y;
    private EasyPickView epv_m;
    private EasyPickView epv_d;
    private TextView tv_sure;
    private String ye;
    private List<String> monthList;
    private List<String> dayList;
    private SureListener sureListener;

    public static MySelectedDialogFragmentTime newInstance(String year, SureListener sureListener) {
        MySelectedDialogFragmentTime f = new MySelectedDialogFragmentTime();
        Bundle bundle = new Bundle();
        bundle.putString("year", year);
        f.setArguments(bundle);
        f.sureListener = sureListener;
        return f;
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.gravity = Gravity.BOTTOM;
        attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(attributes);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.select_dialog_time_ll, container, false);
        //设置窗口以对话框样式显示
//		setStyle(DialogFragment.STYLE_NO_TITLE, R.style.dialog);
        //设置对话框背景色，否则有虚框
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        //设置对话框弹出动画，从底部滑入，从底部滑出
        getDialog().getWindow().getAttributes().windowAnimations = R.style.dialog_animation;
        getDialog().setCancelable(true);
        getDialog().setCanceledOnTouchOutside(true);
        getDialog().getWindow().setGravity(Gravity.BOTTOM);

        tv_y = (TextView) view.findViewById(R.id.tv_y);
        epv_m = (EasyPickView) view.findViewById(R.id.epv_m);
        epv_d = (EasyPickView) view.findViewById(R.id.epv_d);
        tv_sure = (TextView) view.findViewById(R.id.tv_sure);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ye = getArguments().getString("year");
        tv_y.setText(ye);
        monthList = Arrays.asList(getResources().getStringArray(R.array.mon_arr));
        epv_m.setDataList(monthList);
        initDays(0);
        epv_m.setOnScrollChangedListener(new EasyPickView.OnScrollChangedListener() {
            @Override
            public void onScrollChanged(int curIndex) {

            }

            @Override
            public void onScrollFinished(int curIndex) {
                initDays(curIndex);
            }
        });
        tv_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sureListener != null) {
                    sureListener.sure(ye + "-" + epv_m.getValue() + "-" + epv_d.getValue());
                }
                dismissAllowingStateLoss();
            }
        });
    }

    private void initDays(int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, Integer.parseInt(ye.trim()));//先指定年份
        calendar.set(Calendar.MONTH, month);//再指定月份 Java月份从0开始算
        int daysCountOfMonth = calendar.getActualMaximum(Calendar.DATE);//获取指定年份中指定月份有几天
        dayList = new ArrayList<>();
        for (int i = 0; i < daysCountOfMonth; i++) {
            int d = i + 1;
            if (d < 10) {
                dayList.add("0" + d);
            } else {
                dayList.add("" + d);
            }
        }
        epv_d.setDataList(dayList);
    }

    public interface SureListener {
        void sure(String str);
    }
}
